package com.jcj.jcjregistry.http;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface HttpInvoker {
    Logger log = LoggerFactory.getLogger(HttpInvoker.class);


    String post(String json, String url);

    String get(String url);

    HttpInvoker DEFAULT = new OkHttpInvoker(500);

    @SneakyThrows
    static <T> T httpGet(String url, Class<T> tClass) {
        log.debug(" =====>>>>>> httpGet: " + url);
        String result = DEFAULT.get(url);
        log.debug(" =====>>>>>> response: " + result);
        return JSON.parseObject(result, tClass);
    }

    @SneakyThrows
    static <T> T httpPost(String requestString,String url, Class<T> clazz) {
        log.debug(" =====>>>>>> httpGet: " + url);
        String respJson = DEFAULT.post(requestString, url);
        log.debug(" =====>>>>>> response: " + respJson);
        return JSON.parseObject(respJson, clazz);
    }

}
